//
//  BaseTextView.m
//  WeiDianGuanJia
//
//  Created by xieyingze on 2017/5/9.
//  Copyright © 2017年 谢英泽. All rights reserved.
//

#import "BaseTextView.h"
#import "AppHeader.h"

@interface BaseTextView ()

@property (nonatomic, strong) BaseLabel *placeHolderLabel;

@end

@implementation BaseTextView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubView];
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

- (void)initSubView
{
    BaseLabel *placeHolderLabel = [BaseLabel new];
    [placeHolderLabel setLabelStyle:@"" textColor:kCOLOR_RGBValue(0xaaaaaa) textFont:kFONT_mainFont(16.f) texrAlignment:NSTextAlignmentLeft];
    placeHolderLabel.numberOfLines = 0;
    self.placeHolderLabel = placeHolderLabel;
    [self addSubview:placeHolderLabel];
    [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.right.equalTo(@(-5));
        make.top.equalTo(@10);
        make.height.equalTo(@16);
    }];
}

- (void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = placeHolder;
    self.placeHolderLabel.text = placeHolder;
    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@([UILabel labelSpaceHeight:placeHolder width:(kUI_WIDTH - 30) font:12]));
    }];
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    _placeHolderColor = placeHolderColor;
    self.placeHolderLabel.textColor = placeHolderColor;
}

- (void)setTextViewPlaceHolder:(NSString *)placeHolder textColor:(UIColor *)textColor textFont:(UIFont *)textFont textAligment:(NSTextAlignment)textAligment
{
    self.placeHolderLabel.text = placeHolder;
    self.placeHolderLabel.font = textFont;
    self.textColor = textColor;
    self.font = textFont;
    self.textAlignment = textAligment;
    
    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@([UILabel labelSpaceHeight:placeHolder width:(kUI_WIDTH - 30) font:12]));
    }];
}

- (void)textDidChange
{
    self.placeHolderLabel.hidden = self.text.length > 0;
    if (self.complete) {
        self.complete(self.text);
    }
}
@end
